﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Drawing.Imaging;
using ZXing.QrCode.Internal;
using ZXing.QrCode;
using ZXing;
using System.DrawingCore;

namespace XinLife.Controllers.Business
{
    /// <summary>
    /// 打印
    /// </summary>
    public class PrintController : BaseController
    {
        [HttpGet]
        public void getprint()
        {
            //条形码
            newBarcode(BarcodeFormat.CODE_128, "xxxxxxxx",200,100);
            //二维码
            newBarcode(BarcodeFormat.QR_CODE, "wwwwwwwwwww", 200, 200);

        }
        private Bitmap newBarcode(BarcodeFormat format, string text, int width, int height)
        {
            ZXing.ZKWeb.BarcodeWriter writer = new ZXing.ZKWeb.BarcodeWriter();
            writer.Format = format;
            QrCodeEncodingOptions options = new QrCodeEncodingOptions()
            {
                ErrorCorrection = ErrorCorrectionLevel.L,
                DisableECI = true,//设置内容编码
                CharacterSet = "UTF-8", //设置二维码的宽度和高度
                Width = width,
                Height = height,
                PureBarcode = true,  // 设置为纯条码，不带文本,
                Margin = 1//设置二维码的边距,单位不是固定像素
            };
            writer.Options = options;
            var map = writer.Write(text);
            Stream stream = new MemoryStream();
            map.Save(stream, System.DrawingCore.Imaging.ImageFormat.Bmp);
            Bitmap bitmap = new Bitmap(stream);
            return bitmap;
        }
    }
}
